package com.starblink.product.detail.adapter

import android.content.Context
import android.util.Log
import android.util.Size
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.load.DecodeFormat
import com.starblink.android.basic.ext.dp
import com.starblink.android.basic.ext.longClick
import com.starblink.basic.ext.load
import com.starblink.basic.ext.screenWidth
import com.starblink.basic.log.SKLogger
import com.starblink.basic.util.CommUtils
import com.starblink.library.widget.product.ProductCardView
import com.starblink.product.R
import com.youth.banner.adapter.BannerAdapter

class ProductImgAdapter(
    datas: List<String>?,
    val context: Context,
    val mainPicSize: String?,
    val imgContainerH: Int,
    val itemIndex: Int,
    val longClickCall: ((view: View) -> Boolean)? = null,
    val call: () -> Unit,
) :
    BannerAdapter<String, ProductImgAdapter.BannerViewHolder>(datas) {

    //创建ViewHolder，可以用viewType这个字段来区分不同的ViewHolder
    override fun onCreateHolder(parent: ViewGroup?, viewType: Int): BannerViewHolder? {
        val view: View =
            LayoutInflater.from(context).inflate(R.layout.item_image_product, parent, false)
        return BannerViewHolder(view)
    }

    override fun onBindView(holder: BannerViewHolder, data: String?, position: Int, size: Int) {
        setImage(data ?: "", holder.img, holder.item)
        holder.item.setOnClickListener {
            call.invoke()
        }
        holder.img.setOnClickListener {
            call.invoke()
        }
//        holder.img.trackData(
//            page = SpmPageDef.RecommendPage,
//            element = SpmElementDef.RecommendBannerCardV60,
//            params = mapOf(
//                "index" to position.toString()
//            ),
//        )
    }

    class BannerViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var img: ImageView
        var item: ConstraintLayout

        init {
            item = view.findViewById(R.id.rl_content)
            img = view.findViewById(R.id.iv_product)
        }
    }

    /**
     * 若图片尺寸未返回,
    则图片宽度等比缩放至容器宽度后,于容器居中.

    若图片高度<容器高度,且此时容器的剩余空间>62px(判断为扁图和正方形的不长的图),
    则图片宽度等比缩放至容器宽度后,于容器居中

    若图片高度<容器高度,且此时容器的剩余空间<62px(判断为偏长图的图),
    则图片高度等比缩放至容器高度, 于容器居中

    若图片高度>容器高度,且此时图片高度<750px(判断为长图),
    则图片宽度等比缩放至(容器高度-60px)后,于容器居中.

    若图片高度>容器高度,且此时图片高度>750px(判断为超长图),
    则图片高度等比缩放至(容器高度-60px)后,于容器居中.
     */
    private fun setImage(mainPic: String, iv: ImageView, content: ConstraintLayout) {
        iv.run {
            var ratio = 1f
            updateLayoutParams<ConstraintLayout.LayoutParams> {
                if (mainPicSize.isNullOrEmpty()) "1" else mainPicSize.let {
                    try {
                        ratio = it.split("*").let { list ->
                            list[0].toFloat() / list[1].toFloat()
                        }
                        Log.d(
                            "ProductImgAdapter",
                            "==============================$ratio====$mainPicSize++++++$mainPic"
                        )
//                        Log.d(
//                            "ProductImgAdapter",
//                            "++++++++++++$imgContainerH"
//                        )
                    } catch (e: Exception) {
                        SKLogger.e(ProductCardView.TAG, e.message ?: "mainPicSize parse erro")
                    }
                }

//                if (ratio < 0.465) {//超长图
//                    //宽高比相差较大时 增加padding
//                    iv.scaleType = ImageView.ScaleType.CENTER_INSIDE
//                    this.height = ViewGroup.LayoutParams.MATCH_PARENT
//                    content.setPadding(0, 30.dp, 0, 30.dp)
//                } else if (ratio < 0.70) {//长图
//                    iv.scaleType = ImageView.ScaleType.CENTER_CROP
//                    content.setPadding(0, 0, 0, 0)
//                } else if (ratio < 0.78) {//偏长图
//                    iv.scaleType = ImageView.ScaleType.CENTER_CROP
//                    if (itemIndex < 3) {
//                        content.setPadding(0, 10.dp, 0, 10.dp)
//                    } else {
//                        content.setPadding(0, 30.dp, 0, 30.dp)
//                    }
//                } else {
//                    iv.scaleType = ImageView.ScaleType.CENTER_INSIDE
//                    content.setPadding(0, 0, 0, 0)
//                }
            }
            var itemWidth = screenWidth
//        //计算图片宽高
            var size = CommUtils.getImageSize(mainPicSize)
            var imgHeight = getImageH(size, itemWidth)
            if (imgHeight < imgContainerH) {
                if (imgContainerH - imgHeight > 62.dp) {//扁图
                    iv.scaleType = ImageView.ScaleType.CENTER_INSIDE
                    content.setPadding(0, 0, 0, 0)
                    Log.d(
                        "ProductImgAdapter",
                        "=======1==$itemWidth====$imgHeight+++++$imgContainerH"
                    )
//                    iv.scaleType = ImageView.ScaleType.CENTER_CROP
                } else {//偏长图
                    content.setPadding(0, 0, 0, 0)
                    Log.d("ProductImgAdapter", "========2==$imgHeight+++++$imgContainerH")
//                imgW = getImageW(size,imgHeight)
                }
            } else {
                if (imgHeight < 750.dp) {//长图
                    iv.scaleType = ImageView.ScaleType.CENTER_CROP
                    content.setPadding(0, 0, 0, 0)
//                iv.scaleType = ImageView.ScaleType.CENTER_CROP
                    Log.d("ProductImgAdapter", "========3==$imgHeight+++++$imgContainerH")
                } else {//超长图
                    iv.scaleType = ImageView.ScaleType.CENTER_INSIDE
//                    this.height = ViewGroup.LayoutParams.MATCH_PARENT
                    content.setPadding(0, 30.dp, 0, 30.dp)
                    Log.d("ProductImgAdapter", "========4==$imgHeight+++++$imgContainerH")
                }
            }

            load(mainPic, format = DecodeFormat.PREFER_RGB_565)
            longClickCall?.let { call ->
                longClick {
                    call.invoke(it)
                }
            }
        }
//
//        layoutParams.width = itemWidth
//        layoutParams.height = imgHeight
//
//        ImageUtils.loadImage(
//            context,
//            mainPic,
//            imgW, imgHeight,
//            iv,
//        )
    }

    private fun getImageH(size: Size?, imgW: Int): Int {
        var h: Double = imgW.toDouble()
        if (size != null && size.width > 0) {
            h = imgW * ((size.height).toDouble() / (size.width).toDouble())
        }
        return h.toInt()
    }

    /// 长图高度固定后 获取图片宽度
    private fun getImageW(size: Size?, imgH: Int): Int {
        var imgW: Double = imgH.toDouble()
        if (size != null && size.height > 0) {
            imgW = imgH * ((size.width).toDouble() / (size.height).toDouble())
        }
        return imgW.toInt()
    }
}